//
//  UIViewController+RWAdditions.m
//  iconstruction-ios
//
//  Created by Ray on 2023/10/13.
//  Copyright © 2023 81SK. All rights reserved.
//

#import "UIViewController+RWAdditions.h"

@implementation UIViewController (RWAdditions)

@end

@implementation UIViewController (RWNavigation)

- (nullable NSArray<__kindof UIViewController *> *)popViewControllerKillClass:(Class)killClass {
    if (killClass == nil) return nil;
    
    UIViewController *toVC = nil;
    
    NSArray *resultArray = self.navigationController.viewControllers;
    for (NSInteger i = resultArray.count-1; i >= 0; --i) {
        UIViewController *sbVC = resultArray[i];
        if ([sbVC isMemberOfClass:killClass] && i >= 1) {
            toVC = resultArray[i-1];
        }
        if (i <= 1) { break; }
    }
    return [self.navigationController popToViewController:toVC animated:YES];
}

/// 出栈，删除栈内killClass类型的controller
/// @param killClass 控制器类型
/// @param repeat 重复次数
- (nullable NSArray<__kindof UIViewController *> *)popViewControllerToClass:(Class)killClass repeat:(NSInteger)repeat {
    if (killClass == nil) return nil;
    if (repeat <= 0) return nil;
    
    UIViewController *toVC = nil;
    
    NSInteger stepNum = repeat;
    NSArray *resultArray = self.navigationController.viewControllers;
    for (NSInteger i = resultArray.count-1; i >= 0; --i) {
        UIViewController *sbVC = resultArray[i];
        if ([sbVC isMemberOfClass:killClass] && stepNum > 0) {
            toVC = sbVC;
            --stepNum;
        }
        if (stepNum <= 0) { break; }
    }
    return [self.navigationController popToViewController:toVC animated:YES];
}
@end
